package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//用户端 - 地址薄相关接口
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;

    //获取默认地址
    @GetMapping("/defaultAddress")
    public AddressBookResDTO defaultAddress() {
        return addressBookService.defaultAddress();
    }

    @PostMapping
    @ApiOperation("修改默认地址")
    public void updateDefaultAddress(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.updateDefaultAddress(dto);
    }

    @GetMapping("/page")
    public PageResult<AddressBook> findByPage(AddressBookPageQueryReqDTO dto) {
        return addressBookService.findByPage(dto);
    }

    @GetMapping("/{id}")
    public AddressBookResDTO findById(@PathVariable("id") Long id) {
        return addressBookService.findById(id);
    }

    @PutMapping("/{id}")
    public void upById(@RequestBody AddressBookUpsertReqDTO dto, @PathVariable("id") Long id) {
        addressBookService.upById(dto, id);
    }

    @DeleteMapping("/batch")
    public void deleteBatch(@RequestBody Long[] ids) {
        addressBookService.deleteBatch(ids);
    }

    @PutMapping("/default")
    public void updateDefault(@RequestParam("flag") Integer flag, @RequestParam("id") Long id) {
        addressBookService.updateDefault(flag, id);
    }
}